package com.download.zhuaba.Utils;

import com.download.zhuaba.Entity.Music;
import com.download.zhuaba.Service.MusicService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import com.alibaba.fastjson.JSON;

import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
    public static String mp3 = "https://wwwapi.kugou.com/yy/index.php?r=play/getdata&" + "hash=HASH";

    public static String LINK = "https://www.kugou.com/yy/rank/home/PAGE-8888.html?from=rank";


    public static void main(String[] args) throws IOException {

        for (int i = 1; i < 2; i++) {
            String url = LINK.replace("PAGE", i + "");
            getTitle(url);
        }
    }

    public static String getTitle(String url) throws IOException, IOException {
        HttpGetConnect connect = new HttpGetConnect();
        String content = connect.connect(url, "utf-8");
        HtmlManage html = new HtmlManage();
        Document doc = html.manage(content);
        Element ele = doc.getElementsByClass("pc_temp_songlist").get(0);
        Elements eles = ele.getElementsByTag("li");
        ArrayList<Music> musics = new ArrayList<Music>();
        for (int i = 0; i < eles.size(); i++) {
            Element item = eles.get(i);
            String title = item.attr("title").trim();
            String link = item.getElementsByTag("a").first().attr("href");
            System.out.println(link+"---"+title);
            Music music = new Music();
            music.setId(i);
            download(link, title,music);
            musics.add(music);
        }
        String jsonList = JSON.toJSONString(musics);
        System.out.println(jsonList);
        return null;
    }

    public static String download(String url, String name,Music music) throws IOException {
        String hash = "";
        HttpGetConnect connect = new HttpGetConnect();
        String content = connect.connect(url, "utf-8");
        HtmlManage html = new HtmlManage();
        Document document = html.manage(content);
        String imgUrl =document.getElementsByClass("albumImg").get(0).getElementsByTag("img").first().attr("src");
        String songName =document.getElementsByClass("audioName").get(0).attr("title").trim();
        String singer =document.getElementsByClass("singerName fl").get(0).attr("title").trim();
        music.setImageUrl(imgUrl);
        System.out.println(imgUrl);
        music.setTitle(songName);
        music.setSinger(singer);
        String regEx = "\"hash\":\"[0-9A-Z]+\"";
// 编译正则表达式
        Pattern pattern = Pattern.compile(regEx);
        Matcher matcher = pattern.matcher(content);
        if (matcher.find()) {
            hash = matcher.group();
            hash = hash.replace("\"hash\":\"", "");
            hash = hash.replace("\"", "");
        }
        Document doc= Jsoup.connect(url).get();
        String item = mp3.replace("HASH", hash);

        System.out.println(item);
        String mp = connect.connect(item, "utf-8");

        System.out.println(mp);
        JSONObject json = JSONObject.fromObject(mp);
        String album_id = json.getJSONObject("data").getString("recommend_album_id");
        System.out.println(album_id);
        String item1=item+"&album_id="+album_id;
        System.out.println(item1);
        String mp1 = connect.connect(item1, "utf-8");

        System.out.println(mp1);
        JSONObject json1 = JSONObject.fromObject(mp1);
        String playUrl = json1.getJSONObject("data").getString("play_url");
        System.out.println(playUrl + "==");

        music.setSongUrl(playUrl);
        System.out.println(name + "，下载完成");
        return playUrl;
    }


}
